﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Text;

namespace Lawyer.Web
{
    public partial class activity : System.Web.UI.Page
    {
        Lawyer.BLL.ActivityInfo bll = new Lawyer.BLL.ActivityInfo();
        Lawyer.BLL.UserInfo userBll = new Lawyer.BLL.UserInfo();
        public int status = 0;

        public string ArchiveID = "";
        public string status1 = "";
        public string status2 = "";
        public string statusall = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                ArchiveID = Request.QueryString["id"].ToString();
            }
            catch { }
            try
            {
                status = int.Parse(Request.QueryString["status"].ToString());
            }
            catch
            {}
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }

        public string GetLawyerID(string LawyerName)
        {
            Model.UserInfo user = userBll.GetModel(LawyerName);
            return user.UserID;
        }

        public void BindData()
        {
            
            string lawyerID = GetLawyerID(Session["UserName"].ToString());
            string strWhere = "LawyerID = '" + lawyerID + "'";
            if (ArchiveID != "")
            {
                strWhere = strWhere + " and ArchiveID = '"+ArchiveID+"'";
            }
            if (status == 1)
            {
                strWhere = strWhere + " and Status = 1";
                status1 = "current";
            }
            else if (status == 2)
            {
                strWhere = strWhere + " and Status = 2";
                status2 = "current";
            }
            else {
                statusall = "current";
            }

            List<Model.ActivityInfo> list = bll.GetModelList(strWhere);
            ActivityList.DataSource = list;
            ActivityList.DataBind();
            if (ActivityList.Rows.Count != 0)
            {
                ActivityList.HeaderRow.TableSection = TableRowSection.TableHeader;
            }
            else
            {
                btnDelete.Visible = false;
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string idlist = GetSelIDlist();
            if (idlist.Trim().Length == 0)
            {
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "There is no activity chosed!", "activity.aspx");
            }
            else
            {
                bll.DeleteList(idlist);
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "Delete successfully!", "activity.aspx");
            }
        }

        private string GetSelIDlist()
        {
            string idlist = "";
            bool BxsChkd = false;
            for (int i = 0; i < ActivityList.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)ActivityList.Rows[i].FindControl("DeleteThis");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;
                    //#warning 代码生成警告：请检查确认Cells的列索引是否正确

                    if (ActivityList.DataKeys[i].Value != null)
                    {
                        if (idlist.Length == 0)
                        {
                            idlist = idlist + "'" + ActivityList.DataKeys[i].Value.ToString() + "',";
                        }
                        else
                        {
                            idlist = idlist + "'" + ActivityList.DataKeys[i].Value.ToString() + "',";
                        }
                    }
                }
            }
            if (BxsChkd)
            {
                idlist = idlist.Substring(0, idlist.LastIndexOf(","));
            }
            return idlist;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("activityadd.aspx");
        }

        protected void ActivityList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Lawyer.Model.ActivityInfo activity = (Model.ActivityInfo)e.Row.DataItem;
                LinkButton lnkBtn = (LinkButton)e.Row.FindControl("btnStatus");
                Label lbStatus = (Label)e.Row.FindControl("lblStatus");
                if (activity == null)
                {
                    return;
                }
                if (activity.Status == 1)
                {
                    lbStatus.Text = "Ongoing";
                    lnkBtn.Text = "Close?";
                    lnkBtn.Visible = true;
                    lnkBtn.CommandName = "Proceeding";
                }
                if (activity.Status == 2)
                {
                    lbStatus.Text = "Close";    
                }
            }
        }

        protected void ActivityList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Proceeding")
            {
                string activityID = e.CommandArgument.ToString();
                Lawyer.Model.ActivityInfo model = bll.GetModel(activityID);
                if (model != null && model.Status == 1)
                {
                    model.Status = 2;
                    if (bll.Update(model))
                    {
                        Maticsoft.Common.MessageBox.ShowAndRedirect(this, "This activity has finished!", "activity.aspx");
                        return;
                    }
                }
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "status wrong or other error!", "activity.aspx");
            }
        }
    }
}
